package com.qyx.pmpucat.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.qyx.pmpucat.common.result.Result;
import com.qyx.pmpucat.common.result.Results;
import com.qyx.pmpucat.dto.UserListResponseDTO;
import com.qyx.pmpucat.dto.UserQueryDTO;
import com.qyx.pmpucat.entity.User;
import com.qyx.pmpucat.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public Result<User> getCurrentUserInfo() {
        // 获取当前登录用户ID
        Long userId = StpUtil.getLoginIdAsLong();
        
        // 查询用户信息
        User user = userService.getById(userId);
        
        // 清除敏感信息
        if (user != null) {
            user.setPassword(null);
        }
        
        return Results.success(user);
    }

    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    public Result<Page<User>> page(
            @RequestParam(required = false) String realName,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) Integer age,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) Integer gender,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        // 构建查询DTO
        UserQueryDTO queryDTO = new UserQueryDTO();
        queryDTO.setRealName(realName);
        queryDTO.setCity(city);
        queryDTO.setAge(age);
        queryDTO.setPhone(phone);
        queryDTO.setGender(gender);
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);

        return Results.success(userService.getUserPage(queryDTO));
    }
} 